from django.core.handlers.wsgi import WSGIRequest
from django.shortcuts import render
from xml.dom.minidom import parseString
from virtapp.utils.libvirt_manager import virtual


def get_domain(request: WSGIRequest):
    """
    获取主机信息
    """
    virtual_hosts = virtual.open.getAllDomainStats()
    map = []
    if len(virtual_hosts) <= 0:
        return render(request, 'v1/domain.html', {"map": map})

    for i in range(0, len(virtual_hosts)):
        host = {}
        virtual_hosts[i][0].shutdownFlags
        host["state"] = virtual_hosts[i][0].state()
        host["host"] = virtual_hosts[i][0].name()
        host["vcpu"] = virtual_hosts[i][1]["vcpu.current"]
        host["memory"] = str(virtual_hosts[i][0].maxMemory() / 1024 / 1024) + 'G'
        host["desktop"]=parseString(virtual_hosts[i][0].XMLDesc()).getElementsByTagName("graphics")[0].getAttribute("type")
        map.append(host)
    return render(request, 'v1/domain.html', {"map": map})